<?php
/**
 * 模板相关函数 - JBlog
 *
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: func_template.php 551 2010-07-22 09:35:52Z lisijie86 $
*/

!defined('IN_JBLOG') && exit('Access Denied!');

require_once JBLOG_INC.'func_post.php';

/**
 * 显示标签云
 * 
 * @param array $tags 标签
 */
function show_tag_cloud ($tags) {
	$font_maxsize = 30;
	$font_minsize = 10;
	$step = 2;
	$output = '';
	foreach ( $tags as $tag ) {
		$fontsize = min($font_minsize + ($tag['count']/$step), $font_maxsize);
		$style = "font-size:{$fontsize}pt;";
		$output .= ' <a href="'.url('mod=tags&tagname='.$tag['tagname']).'" title="'.$tag['count'].' posts" style="'.$style.'">'.$tag['tagname'].'</a> ';
	}
	echo $output;
}

/**
 * 底部版权信息
 */
function copyright() {
	echo 'Powered by <a href="http://www.lisijie.org" target="_blank" title="JBlog official website">JBlog '.JBLOG_VERSION.'</a>. &copy '.get_date(NOW,'Y').' '.config('blogname').'. ';
}

/**
 * 输出博客运行信息
 */
function runinfo() {
	global $db;
	echo 'Processed in '.exec_time().' second(s), '.$db->querynum.' queries.';
}

/**
 * 输出博客信息，用于模板
 *
 * 一个参数为取值，两个参数为赋值，支持过滤器，过滤器tag为：blog_info_键名
 *
 * @param string $key  键名
 * @param bool $return 是否返回值
 * @return $return为true时返回值，否则直接输出
 */
function blog($key) {
	global $_CACHE;
	if ( func_num_args() == 1 ) {
		if ( isset($_CACHE['_tpl'][$key]) ) {
			$value = apply_filter("blog_info_{$key}", $_CACHE['_tpl'][$key]);
		} elseif ( $_CACHE['config'][$key] ) {
			$value = apply_filter("blog_info_{$key}", $_CACHE['config'][$key]);
		}
		echo $value;
	} else {
		$value = func_get_arg(1);
		if ( $key == 'title' ) {
			$_CACHE['_tpl'][$key] = $value . ' &laquo; ' . config('blogname');
		} else {
			$_CACHE['_tpl'][$key] = $value;
		}
	}
}

/**
 * 页面列表
 *
 * 可用参数：cur_css,num,ids,parentid
 *
 * @param string $params querystring形式的参数列表
 * @return void
 */
function list_pages($params='') {
	global $cache, $get, $db;
	parse_str($params,$params);
	$wheresql = '';
	if ( $params['ids'] ) {
		$params['ids'] = explode(',',$params['ids']);
		filter_ids($params['ids']);
		if ( $params['ids'] ) {
			$wheresql .= " AND id IN (".simplode($params['ids']).")";
		}
	}
	if ( $params['parentid']) {
		$wheresql .= " AND parendid = '".intval($params['parentid'])."'";
	}
	$sql = "SELECT id,alias,title FROM ".tname('post')." WHERE `type` = 'page' AND `status` = 1 $wheresql";
	if ( is_numeric($params['num']) ) {
		$sql .= "LIMIT {$params['num']}";
	}
	$result = array();
	$query = $db->query($sql);
	while ( $row = $db->fetch_array($query) ) {
		$result[] = $row;
	}
	$output = '';
	$i = 0;
	foreach ($result as $row) {
		$curstr = '';
		if ( $get['mod'] == 'page' && post('id', true) == $row['id'] ) {
			$curstr = ' class="'.$params['cur_css'].'"';
		}
		$output .= '<li'.$curstr.'><a href="'.url('mod=page&id='.$row['id'].'&alias='.$row['alias']).'" title="'.$row['title'].'">'.$row['title'].'</a></li>';
	}
	echo $output;
}

/**
 * 分类列表
 *
 * 可用参数：cur_id,cur_css
 *
 * @param string $params querystring形式的参数列表
 * @return void
 */
function list_cates($params='') {
	global $_CACHE;
	parse_str($params,$params);
	$output = '';
	foreach ( $_CACHE['cate']['blog'] as $cateid ) {
		$cate = $_CACHE['cate'][$cateid];
		$curstr = '';
		if ( $cate['id'] == $params['cur_id'] ) {
			$curstr = ' class="'.$params['cur_css'].'"';
		}
		if ( !empty($cate['redirect']) ) {
			$url = $cate['redirect'];
		} else {
			$url = url('mod=cate&id='.$cate['id'].'&alias='.$cate['alias']);
		}
		$output .= '<li'.$curstr.'><a href="'.$url.'">'.$cate['catename'].'</a></li>';
	}
	echo $output;
}

/**
 * 获取用户信息
 *
 * @param string $key  键名
 */
function user($key) {
	return $GLOBALS['_USER'][$key];
}

/**
 * 显示已启用的Widget
 *
 */
function list_widgets($setting = array()) {
	$default = array(
		'widget_before' => '<div class="widget widget_%s">',
		'widget_after' => '</div>',
		'title' => '<h2 class="widget_title widget_%1$s_title">%2$s</h2>',
	);
	$setting = array_merge($default, $setting);
	$widgets = config('widgets');
	if ( $widgets ) {
		$widgets = unserialize($widgets);
	}
	foreach ( (array)$widgets as $name ) {
		$widget = Widget::factory($name);
		if ( false !== $widget ) {
			echo sprintf($setting['widget_before'], $widget->id);
			echo sprintf($setting['title'], $widget->id, $widget->get_option('title'));
			echo $widget->display();
			echo sprintf($setting['widget_after']);
		}
	}
}

function load_theme_javascript() {
	$blogurl = config('blogurl');
	$output .= "<script type=\"text/javascript\" src=\"{$blogurl}/script/jquery.js\"></script>
<script type=\"text/javascript\" src=\"{$blogurl}/script/jquery.highlighter.js\"></script>
<script type=\"text/javascript\" src=\"{$blogurl}/script/common.js\"></script>
<script type=\"text/javascript\">
	var blog_url = '{$blogurl}';
	var image_resize_width = '".config('image_resize_width')."';
	\$(function(){
		\$.SyntaxHighlighter('{$blogurl}/script/SyntaxHighlighter/');
	});
</script>
";
	echo $output;
}
add_action('theme_head', 'load_theme_javascript');

//分页
function showpage ($pagenum = 10) {
	global $count, $pagesize, $page, $url;
	$pagestr = '';
	if ( false !== ($pos = strpos($url,'?')) ) {
		$url = substr($url,$pos+1);
	}
	$url .= '&';
	$realpages = 1;
	if ($count > $pagesize) {
		$realpages = @ceil($count/$pagesize);
		if ($realpages < $pagenum) {
			$from = 1; $to = $realpages;
		} else {
			$offset = 5;
			$from = $page - $offset;
			$to = $from + $pagenum;
			if ($from < 1) {
				$from = 1;
				$to = $from + $pagenum - 1;
			} elseif ($to > $realpages) {
				$to = $realpages;
				$from = $realpages - $pagenum + 1;
			}
		}
		$pagestr .= '<span>'.$count.'</span>';
		if ($page > $pagenum) $pagestr .= '<a href="'.url($url.'page=1').'">1...</a>';
		if ($page > 1) $pagestr .= '<a href="'.url($url.'page='.($page-1)).'">&lsaquo;&lsaquo;</a>';
		for ($i = $from; $i <= $to; $i++) {
			if ($i == $page) {
				$pagestr .= '<span class="on">'.$i.'</span>';
			} else {
				$pagestr .= '<a href="'.url($url.'page='.$i).'">'.$i.'</a>';
			}
		}
		if ($page < $realpages) $pagestr .= '<a href="'.url($url.'page='.($page+1)).'">&rsaquo;&rsaquo;</a>';
		if ($realpages > $to) $pagestr .= '<a href="'.url($url.'page='.$realpages).'">'.$realpages.'...</a>';
		//if ($realpages > 1 && !config('rewrite')) $pagestr .= '<input type="text" size="3" class="go" onkeydown="if (event.keyCode==13) {window.location=\''.$url.'page=\'+this.value;return false;}" />';
		$pagestr = '<div class="multipage">'.$pagestr.'</div>';
	}
	return $pagestr;
}

/**
 * 预设变量
 */
blog('themeurl', config('blogurl').'/themes/'.config('theme'));
?>